#include<iostream>
#include<queue>
#include<string>
using namespace std;
const int N = 2010;
struct node
{
	int id, weight;
	friend bool operator<(node a, node b) {
		if (a.weight == b.weight) {
			return a.id > b.id;
		}
		else {
			return a.weight < b.weight;
		}
	}
};
int main()
{
	int t;
	while (cin >> t) {
		priority_queue<node> q[N];
		int count = 1;
		while (t--) {
			string s;
			cin >> s;
			if (s[0] == 'I') {
				int doctor, weight;
				cin >> doctor >> weight;
				q[doctor].push({ count,weight });
				count++;
			}
			else {
				int doctor;
				cin >> doctor;
				if (q[doctor].size() == 0) {
					cout << "EMPTY" << endl;
				}
				else {
					int x = q[doctor].top().id;
					q[doctor].pop();
					cout << x<< endl;
				}
			}
		}
	}
	return 0;
}